<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">編輯</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">刪除</a>
        </script>

    </div>
</div>

<script>
    var deailId = 0;
    var pid = 0;
    layui.use(['form', 'table','miniPage','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;

        pid = getQuery('pid');
        pid = pid ? pid : 0;
        table.render({
            elem: '#currentTableId',
            url: buildUrl(urlInfo.page.list, {type:"page"}),
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter'],
            cols: [[
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'name', minWidth: 150, title: '顯示名稱'},
                {field: 'type', minWidth: 80, title: '類型'},
                {field: 'tag_key', width: 150, title: '關鍵標識'},
                //{field: 'url', width: 150, title: '連接'},
                {field: 'sort_order', title: '排序', width: 80},
                {field: 'status', width: 80, title: '使用', sort: true},
                {field: 'publish_at', width: 120, title: '發布時間', sort: true},
                {title: '操作', minWidth: 180, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 20,
            page: true,
            skin: 'line'
        });

        // 監聽搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最終的搜索信息'
            });

            //執行搜索重載
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar事件監聽
         */
        table.on('toolbar(currentTableFilter)', async function (obj) {
       
            if (obj.event === 'add') {   // 監聽添加操作
                var content = miniPage.getHrefContent('page/page/add.html');
                var openWH = miniPage.getOpenWidthHeight();
                
                openWH[2] = 0;
                var index = layer.open({
                    title: '添加',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end:function(){
                        // location.reload();
                        table.reload('currentTableId', {
                            page: {
                                curr: 1
                            }
                            , where: {
                                searchParams: {}
                            }
                        }, 'data');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 監聽刪除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                var arr = new Array();
				for(var x of data){
					arr.push(x.id);
				}
				layer.confirm('Confirm the deletion of this data？', async function (index) {
					var info = await ajaxPost($, buildUrl(urlInfo.page.delete), {id:arr});
                    if(info.code==4004){
                        alert(info.message);
                    }else{
                        obj.del();
                    }
                    layer.close(index);
					if(info.code==4004){
						alert(info.message);
					}else{
						if(info.status){
							//执行搜索重载
							table.reload('dataProvider', {
								page: {
									curr: 1
								}
								, where: {
									searchParams: {}
								}
							}, 'data');
						}
						if(info.message){
							var index2 = layer.alert(info.message, {
								title: 'Information'
							}, function () {
								// 關閉彈出層
								layer.close(index2);								
							});
						} 
					}
					layer.close(index);
					
				});
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            deailId = data.id
            if (obj.event === 'edit') {

                var content = miniPage.getHrefContent('page/page/edit.html?id='+data.id);
                var openWH = miniPage.getOpenWidthHeight();
                openWH[2] = 0;
                var index = layer.open({
                    title: '編輯',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end:function(){
                        // location.reload();
                        table.reload('currentTableId', {
                            page: {
                                curr: 1
                            }
                            , where: {
                                searchParams: {}
                            }
                        }, 'data');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('確認冊除該數據？', async function (index) {
                    var info = await ajaxPost($, buildUrl(urlInfo.page.delete), {id:data.id});
                    if(info.code==4004){
                        alert(info.message);
                    }else{
                        obj.del();
                    }
                    layer.close(index);
                    
                });
            }
        });
		function timeAdd() {
            lay('.date-select').each(function() {
                laydate.render({
                    elem: this,
                    lang: 'en',
                    type: 'datetime',
                    trigger: 'click',
                    format: 'yyyy-MM-dd'
                });
            });
            $('.date-select').removeAttr('lay-key');
            $('.date-select').removeAttr('layui-paydate-id');
        }
        timeAdd();
    });
</script>